package com.example.text;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private TextView tversion;

    private String oldName = "old.apk";
    private String newName = "new.apk";

    static {
        System.loadLibrary("native-lib");
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tversion = findViewById(R.id.version);

        tversion.setText(BuildConfig.VERSION_NAME);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            String[] parms = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
            if (checkSelfPermission(parms[0]) == PackageManager.PERMISSION_DENIED) {
                requestPermissions(parms, 200);
            }
        }
    }

    /**
     * 点击事件
     *
     * @param view
     */

    public void toGetApk(View view) {

        hbApk();
    }

    @SuppressLint("StaticFieldLeak")
    private void hbApk() {
        new AsyncTask<Void, Void, File>() {
            /**
             * 耗时
             * @param voids
             * @return
             */
            @Override
            protected File doInBackground(Void... voids) {
                String oldapk = getApplication().getApplicationInfo().sourceDir;
                //从网络行下载的差分包在本地的路径
                String patch = new File(Environment.getExternalStorageDirectory(), "a.patch").getAbsolutePath();
                //放置最新包的路径
                String output = creatNewApk(newName).getAbsolutePath();
                //调用native方法
              //  bsPatch(oldapk, patch, output);
                bsPatchApk(oldapk, patch, output);

                return new File(output);
            }

            /**
             * 耗时操作完成之后 进这个方法
             * @param file
             */
            @Override
            protected void onPostExecute(File file) {
                super.onPostExecute(file);
                 UriParseUtils.installApk(MainActivity.this, file);
            }

            @Override
            protected void onProgressUpdate(Void... values) {
                super.onProgressUpdate(values);
            }
        }.execute();
    }

    /**
     * 创建路径
     *
     * @return
     */
    private File creatNewApk(String name) {
        File newapk = new File(Environment.getExternalStorageDirectory(), name);

        try {
            if (!newapk.exists()) {
                newapk.createNewFile();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return newapk;

    }


    /**
     * @param oldApk 老板本
     * @param path   差分包
     * @param output 新包
     * @return
     */


    public native int bsPatchApk(String oldApk, String path, String output);


    @Override
    protected void onRestart() {
        super.onRestart();
        tversion.setText(BuildConfig.VERSION_NAME);
    }


}
